﻿using System;
using System.Threading;
using Mido.Domain;
using Mido.UiModules.SettingsModule;

namespace Mido.UiModules.ActiveImageModule
{
    internal class ActiveImageModel
    {
        internal delegate void ActiveImageChangedDelegate(ProcessedImage oldValue, ProcessedImage newValue);

        internal event ActiveImageChangedDelegate ActiveImageChanged;
        internal event ThreadStart ShowPrivCalled;
        internal event ThreadStart ShowNextCalled;
        internal event ThreadStart PositionInCollectionChanged;
        internal event ThreadStart ExcludeActiveImage;

        private readonly SettingsModel _settings;

        private ProcessedImage _activeImage;

        private bool _isFirstImage;
        private bool _isLarstImage;

        public ActiveImageModel(SettingsModel settings)
        {
            if (settings == null) 
                throw new ArgumentNullException("settings");

            _settings = settings;
        }

        internal ProcessedImage ActiveImage
        {
            get { return _activeImage; }
            set
            {
                if (_activeImage == value)
                    return;

                ProcessedImage oldValue = _activeImage;
                ProcessedImage newValue = value;

                _activeImage = newValue;

                if (ActiveImageChanged != null)
                    ActiveImageChanged(oldValue, newValue);
            }
        }

        internal SettingsModel Settings
        {
            get { return _settings; }
        }

        public bool IsFirstImage
        {
            get { return _isFirstImage; }
            set
            {
                if (_isFirstImage == value)
                    return;

                _isFirstImage = value;
                
                if (PositionInCollectionChanged != null)
                    PositionInCollectionChanged();
            }
        }

        public bool IsLastImage
        {
            get { return _isLarstImage; }
            set
            {
                if (_isLarstImage == value)
                    return;

                  _isLarstImage = value;

                if (PositionInCollectionChanged != null)
                    PositionInCollectionChanged();
            }
        }

        internal void OnShowPriv()
        {
            if (ShowPrivCalled != null)
                ShowPrivCalled();
        }

        internal void OnShowNext()
        {
            if (ShowNextCalled != null)
                ShowNextCalled();
        }

        internal void OnClearCustomPosition()
        {
            _activeImage.CustomPosition = null;
        }

		internal void OnRotateRight()
		{
			switch (_activeImage.RotateAngle)
			{
				case WatermarkAngle.a0:
					_activeImage.RotateAngle = WatermarkAngle.a90;
					break;
				case WatermarkAngle.a90:
					_activeImage.RotateAngle = WatermarkAngle.a180;
					break;
				case WatermarkAngle.a180:
					_activeImage.RotateAngle = WatermarkAngle.a270;
					break;
				case WatermarkAngle.a270:
					_activeImage.RotateAngle = WatermarkAngle.a0;
					break;
			}
		}

		internal void OnRotateLeft()
		{
			switch (_activeImage.RotateAngle)
			{
				case WatermarkAngle.a0:
					_activeImage.RotateAngle = WatermarkAngle.a270;
					break;
				case WatermarkAngle.a90:
					_activeImage.RotateAngle = WatermarkAngle.a0;
					break;
				case WatermarkAngle.a180:
					_activeImage.RotateAngle = WatermarkAngle.a90;
					break;
				case WatermarkAngle.a270:
					_activeImage.RotateAngle = WatermarkAngle.a180;
					break;
			}
		}

		internal void OnExclude()
	    {
			if (ExcludeActiveImage != null)
				ExcludeActiveImage();
	    }
    }
}
